package com.chen.jdbcdemo;

import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
@Component
public class OperMysql {
    /**
     * 原生jdbc写 查询和删除
     * @return
     */

    public Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
//        String url="jdbc:mysql://140.143.124.123:3307/chen?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true";
            String url = "jdbc:mysql://140.143.124.123:3306/chen?characterEncoding=utf-8";
            String username = "cgj";
            String password = "cgj666";
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    public void selectAll(Connection connection) throws Exception{
        String sql = " select  * from t_user";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            System.out.println(resultSet.getString("uid") +"--"+resultSet.getString("name") + "-----" + resultSet.getString("password"));
        }
    }
    public Integer deleteById(Connection connection,Long id) throws Exception{
        String sql = " delete from t_user  where uid="+id;
        Statement statement = connection.createStatement();
        int result = statement.executeUpdate(sql);
        return result;
    }

}
